﻿using System.Linq;
using System.Text;
using System;
using System.IO;
using VH6I_B4.ProductParsing;
using System.Collections.Generic;
using System.Collections;
using VH6I_B4.Datawarehouse;
using MySql.Data.MySqlClient;

namespace VH6I_B4.BronBestandReaders
{
    class sqlReader : ABronBestandReader
    {
        private string _supermarket;
        private ProductParser _productParser;

        public sqlReader(DatawarehouseApi dwh)
            : base(dwh)
        {
        }

        public override void read()
        {
            string connString = "SERVER=" + "localhost" + ";" +
                "DATABASE=Koffiebestanden;" +
                "UID=root;" +
                "PASSWORD=;";
            MySqlConnection cnMySQL = new MySqlConnection(connString);
            MySqlCommand cmdMySQL = cnMySQL.CreateCommand();
            MySqlDataReader SQLreader;
            cmdMySQL.CommandText = "select * from coffeedata";
            cnMySQL.Open();
            SQLreader = cmdMySQL.ExecuteReader();
            parseSQLsheet(SQLreader);
            cnMySQL.Close();
        }

        public void parseSQLsheet(MySqlDataReader SQLreader)
        {
            while (SQLreader.Read())
            {
                string[] currentRow = parseRow(SQLreader);

                if (_supermarket == null || _supermarket != currentRow[0])
                {
                    _supermarket = currentRow[0];
                    _productParser = new ProductParser();
                }

                Dictionary<ProductLevel, string> parsedProduct = _productParser.Parse(currentRow[2]);
                if (parsedProduct.ContainsKey(ProductLevel.BARCODE))
                {
                    int dgdfg = 0;
                }
            }
        }

        private string[] parseRow(MySqlDataReader mysqlReader)
        {
            string[] row = new string[13];
            row[0] = mysqlReader.GetString(0);
            row[1] = mysqlReader.GetString(1);
            row[2] = mysqlReader.GetString(2);
            row[3] = mysqlReader.GetString(3);
            row[4] = mysqlReader.GetString(4);
            row[5] = mysqlReader.GetString(5);
            row[6] = mysqlReader.GetString(6);
            row[7] = mysqlReader.GetString(7);
            row[8] = mysqlReader.GetString(8);
            row[9] = mysqlReader.GetString(9);
            row[10] = mysqlReader.GetString(10);
            row[11] = mysqlReader.GetString(11);
            row[12] = mysqlReader.GetString(12);
            return row;
        }
    }
}
